package cn.org.xuanguang.kepler.hormos.orm.cmd;

import cn.org.xuanguang.kepler.core.service.Result;
import cn.org.xuanguang.kepler.hormos.orm.result.PagerResult;
import cn.org.xuanguang.kepler.hormos.orm.service.GenericCrudIService;
import cn.org.xuanguang.kepler.hormos.rdb.dml.operator.delete.DeleteOperatorResulet;
import cn.org.xuanguang.kepler.hormos.rdb.dml.operator.insert.InsertOperatorResulet;
import cn.org.xuanguang.kepler.hormos.rdb.dml.operator.update.UpdateOperatorResulet;
import cn.org.xuanguang.kepler.hormos.rdb.dsl.param.QueryParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.beetl.sql.core.page.PageResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @param <E>
 * @param <K>
 */
public abstract class BaseCmd<E, K> implements ICmd<E, K> {


    public static Logger logger = LoggerFactory.getLogger(BaseCmd.class);
    private GenericCrudIService<E, K> service;

    public abstract GenericCrudIService<E, K> getService();

    //    @PatchMapping
//    @Operation(summary = "保存数据", description = "如果传入了id,并且对应数据存在,则尝试覆盖,不存在则新增.")
//    default SaveResult save(@RequestBody E payload) {
//        return getService().save(payload);
//    }
    // 新增
    @ApiResponses(value = {@ApiResponse(responseCode = "400", description = "Invalid IENTITY supplied"),
            @ApiResponse(responseCode = "404", description = "IENTITY not found")})
    @PostMapping("/insert")
    @Operation(summary = "新增数据", description = "只新增，不校验id是否存在.")
//    @EasyLog(module = "di", type = OperateType.ADD,
//            success = " {{#entity}} insert成功",
//            bizNo = "insert",
//            condition = "{{#entity}}",
//            fail = " {{#entity}} insert失败"
//    )
    public Result<InsertOperatorResulet> insert(@RequestBody E payload) {
        return Result.OK(getService().insert(payload));
    }

    //修改
    @Operation(summary = "修改数据", description = "根据id修改数据.")
    @PostMapping("/update")
//    @EasyLog(module = "di", type = OperateType.UPDATE,
//            success = " {{#entity}} update成功",
//            bizNo = "update",
//            condition = "{{#entity}}",
//            fail = " {{#entity}} update失败"
//    )
//    @ResourceAction(code = Permission.ACTION_UPDATE, name = "修改")
    public Result<UpdateOperatorResulet> updateById(@RequestBody E payload) {
        return Result.OK(getService().updateById(payload));
    }

    /**
     * 明细
     *
     * @return
     */
    @GetMapping("/detail/{id}")
    @Operation(summary = "明细", description = "根据id查询明细.")
    public Result<E> queryById(@PathVariable K primaryKey) {
        return Result.OK(getService().queryById(primaryKey));
    }

    // 全查询
    @GetMapping("/getAll")
    @Operation(summary = "全查询", description = "查询所有数据.")
//    @EasyLog(module = "di", type = OperateType.SELECT,
//            success = "getAll成功",
//            bizNo = "getAll",
//            fail = " getAll失败"
//    )
    public Result<List<E>> queryAll() {
        return Result.OK(getService().queryAll());
    }

    @DeleteMapping("/delete/{id}")
//    @EasyLog(module = "di", type = OperateType.DELETE,
//            success = "delete成功",
//            condition = "{{#id}}",
//            bizNo = "delete",
//            fail = "delete失败"
//    )
//    @ResourceAction(code = Permission.ACTION_DELETE, name = "删除")
    public Result<DeleteOperatorResulet> deleteById(@PathVariable K id) {
        return Result.OK(getService().deleteById(id));
    }

    @DeleteMapping("/delete/")
//    @EasyLog(module = "di", type = OperateType.DELETE,
//            success = "delete成功",
//            condition = "{{#id}}",
//            bizNo = "delete",
//            fail = "delete失败"
//    )
//    @ResourceAction(code = Permission.ACTION_DELETE, name = "删除")
    public Result<DeleteOperatorResulet> delete(@RequestBody E payload) {
        return Result.OK(getService().delete(payload));
    }

    // 分页
    @PostMapping("/query")
    @Operation(summary = "分页", description = "分页查询.")
    public Result<PagerResult<E>> query(QueryParam queryParam) {
        return Result.OK(getService().query(queryParam));
    }

    /**
     * 分页数据
     *
     * @return
     */
    @GetMapping("/query_get")
//    @EasyLog(module = "di", type = OperateType.SELECT,
//            success = "query成功",
//            condition = "{{#terms}}",
//            bizNo = "query",
//            fail = "query失败"
//    )
    @Operation(description = "分页查询get方法,如果要调用encodeParam方法，需要走这个接口")
//    @ResourceAction(code ="Query_Get", name = "分页查询get方法")
    public Result<PageResult<E>> queryGet(QueryParam queryParam) {
//        checkTerms(terms);
        return Result.OK(getService().query(queryParam));
    }

}
